package com.abstractgames;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class Screen {
	public static int WIDTH = 960;
	public static int HEIGHT = 640;
	
	private int MAXY = Physics.MAXY;
	private int MAXX = Physics.MAXX;
	
	public static int CAMHEIGHT = HEIGHT;
	public static int CAMWIDTH = WIDTH;
	private int FIELDHEIGHT = Physics.FIELDHEIGHT;
	private int FIELDWIDTH = Physics.FIELDWIDTH;
	
//	private Mesh mesh;
//	private Rectangle glViewport;
//	private float rotationSpeed;
	
	private SpriteBatch batch;
	private Texture texture;
	private TextureRegion[] balls = new TextureRegion[5];
	private TextureRegion cross;
	private BitmapFont font;
	public static OrthographicCamera camera;
	public static int test = 0;
	
	Screen(){
//		rotationSpeed = 0.5f;
		
//		mesh = new Mesh(true, 4, 6,
//					new VertexAttribute(VertexAttributes.Usage.Position, 3,"attr_Position"),
//					new VertexAttribute(Usage.TextureCoordinates, 2, "attr_texCoords"));
//		texture = new Texture(Gdx.files.internal("data/sc_map.png"));
//		mesh.setVertices(new float[] { 
//				-1024f, -1024f, 0, 0, 1,
//				1024f, -1024f, 0, 1, 1,
//				1024f,  1024f, 0, 1, 0,
//				-1024f,  1024f, 0, 0, 0
//		});
//		mesh.setIndices(new short[] { 0, 1, 2, 2, 3, 0 });

		camera = new OrthographicCamera(CAMWIDTH, 	CAMHEIGHT);            
		camera.position.set(FIELDWIDTH / 2, FIELDHEIGHT / 2, 0);

//		glViewport = new Rectangle(0, 0, 2*WIDTH, 2*HEIGHT);

		batch = new SpriteBatch();
		texture = new Texture(Gdx.files.internal("res/particles2.png"));
		
		for(int i=0; i<4; i++){
		balls[i] = new TextureRegion(texture,i*32,0,32,32);
		}
		balls[4] = new TextureRegion(texture,0,32,32,32);
		cross = new TextureRegion(texture,0,96,32,32);
			
		font = new BitmapFont(Gdx.files.internal("res/font16.fnt"), Gdx.files.internal("res/font16.png"), false);
	}
	
	public void tick(){
		float x = camera.position.x-CAMWIDTH/2;
		float y = camera.position.y-CAMHEIGHT/2;
		
		GL10 gl = Gdx.graphics.getGL10();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glClearColor(1,1,1,1);
		      
      // Camera --------------------- /
//		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//		gl.glViewport((int) glViewport.x, (int) glViewport.y,
//						(int) glViewport.width, (int) glViewport.height);

		camera.update();
		camera.apply(gl);
		batch.setProjectionMatrix(camera.combined);
		
		batch.begin();
		font.setColor(0,0,0,1);
		
//		font.draw(batch,"Nx:"+(int)Math.floor(Gdx.input.getX()/32),x+5,y+25);
//		font.draw(batch,"Ny:"+(int)Math.floor((HEIGHT-Gdx.input.getY())/32),x+80,y+25);
//		font.draw(batch,"x:"+Gdx.input.getX(),x+5,y+50);
//		font.draw(batch,"y:"+(HEIGHT-Gdx.input.getY()),x+80,y+50);
//		font.draw(batch,"state:"+Input.button+" FPS:"+(int)(1/Gdx.graphics.getDeltaTime())+"   "+camera.zoom,x+160,y+25);
		
		font.draw(batch,"Nx:"+(int)Math.floor(Input.X/32),x+5,y+25);
		font.draw(batch,"Ny:"+(int)Math.floor(Input.Y/32),x+100,y+25);
		font.draw(batch,"x:"+Input.X,x+5,y+50);
		font.draw(batch,"y:"+Input.Y,x+100,y+50);
		font.draw(batch,"state:"+Input.button+" FPS:"+(int)(1/Gdx.graphics.getDeltaTime())+"   "+camera.zoom,x+200,y+25);
		if(!Game.running) font.draw(batch,"PAUSED",WIDTH/2,HEIGHT/2);
		
		batch.setColor(1, 1, 1, 1);
		for(int i=-1; i<MAXX; i++)
			for(int j=-1; j<MAXY; j++)
			batch.draw(cross, 16 + 32*i, 16 + 32*j);
		
		batch.setColor(0, 0, 1, 1);
		for (int i = 0; i < Physics.atoms.size(); i++) {
			Atom atom = Physics.atoms.get(i);
			if(atom.collided) batch.setColor(1, 0, 0, 1);
				else batch.setColor(0, 0, 1, 1);
			batch.draw(balls[atom.O], 32*atom.X, 32*atom.Y);}

		batch.end();
		
	}
	
}